{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "123f2052-98cf-4d3a-8ca7-00c5422e7ac7",
   "metadata": {},
   "source": [
    "ndarray对象的属性\n",
    "ndarray 是一个多维数组对象，由两部分构成：\n",
    "1、实际的数据 2、描述这些数据的元数据（数据维度、数据类型等）\n",
    "\n",
    "ndarry 实例\n",
    "np.array() 生成一个ndarray数组，ndarray在程序中的别名是array\n",
    "array(object,dtype=None,copy=bool,order='k',subok:bool,ndmin:int,like；array_like)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dd61d40-edb6-4082-bc1c-2749f92b7acd",
   "metadata": {},
   "source": [
    "np.arange()函数返回一个有终点和起点的固定步长的排序，类似python中的range（）函数，不过这里支持浮点型\n",
    "arange([start,],stop[,step],dtype=None,*,like=None)\n",
    "\n",
    "nidm:秩，即轴的数量或维度的数量\n",
    "shape：ndarray对象尺度，对应矩阵n行m列\n",
    "size：ndarray对象元素的个数，相当于shape中n*m的值\n",
    "dtype：ndarray对象的元素类型\n",
    "itensize：ndarray对象中每个元素的大小，以字节为单位\n",
    "\n",
    "np.linspace()根据起止数据间距的填充数据，形成数组，等差数列\n",
    "np.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None)\n",
    "\n",
    "np.logspace()是对数等比数列创建函数\n",
    "np.logspace(start,stop,num=50,endpoint=True,base=10.0,dtype=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7a27e1b4-b11e-453c-b9b3-5eacaf2dfee6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 5) 10 int32 4\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[0,1,2,3,4],[5,6,7,8,9]])\n",
    "print(a.shape,a.size,a.dtype,a.itemsize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a06264b4-cff9-45b0-8d46-35c2191527ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5]\n",
      "[2 3 4 5]\n",
      "[1 3 5]\n"
     ]
    }
   ],
   "source": [
    "print(np.arange(6))\n",
    "print(np.arange(2,6))\n",
    "print(np.arange(1,6,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a1073ef4-dc0d-43ef-9e89-552394b1cc70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.   0.12 0.24 0.36 0.48 0.6 ]\n"
     ]
    }
   ],
   "source": [
    "print(np.linspace(0,0.6,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "076cbba3-3faa-4bbf-8b8c-ef23c4687f55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 4., 8.])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(0,3,4,base = 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "15977fbe-89cf-44ae-953c-8e90b40337ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "<class 'numpy.ndarray'>\n",
      "(3, 4)\n",
      "12\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "data = np.arange(12).reshape(3,4)  # 创建一个三行四列的数组\n",
    "print(data)\n",
    "print(type(data))\n",
    "\n",
    "print(data.shape)\n",
    "print(data.size)\n",
    "print(data.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "55744265-6065-45c0-b72f-a406f58566c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "[[1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]]\n",
      "[[1.04522281e-311 1.77863633e-322]\n",
      " [0.00000000e+000 0.00000000e+000]\n",
      " [1.16636714e+200 3.34881137e-061]\n",
      " [1.16188947e+165 5.28206494e-091]\n",
      " [3.08798462e-057 7.69843790e+218]]\n",
      "<class 'numpy.ndarray'>\n",
      "float64\n",
      "[ 1  6 11 16]\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "# 使用不同的方法来对数组进行处理操作\n",
    "import numpy as np\n",
    "# 通过zeros（）函数创建元素值都是零的数组\n",
    "np_zeros = np.zeros((3,4))\n",
    "print(np_zeros)\n",
    "# 通过调用ones（)函数创建元素值都是一的数组\n",
    "np_ones = np.ones((3,4))\n",
    "print(np_ones)\n",
    "# empty（）函数创建一个新的数组，该数组只分配了内存空间，它里面填充的元素都是随机的，且数据类型默认为float64\n",
    "np_empty = np.empty((5,2))\n",
    "print(np_empty)\n",
    "print(type(np_empty))\n",
    "print(np_empty.dtype)\n",
    "# 通过arange（）函数可以创建一个等差数列\n",
    "np_arange = np.arange(1,20,5)\n",
    "print(np_arange)\n",
    "# 通过name属性可以访问获取数据类型\n",
    "data_one = np.array([[1,2,3],[4,5,6]])\n",
    "print(data_one.dtype.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a01a2745-3088-41da-a80a-64ecc63e3f21",
   "metadata": {},
   "source": [
    "转换数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9751ef05-5520-4ba0-9209-9719549bdf19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n",
      "float64\n"
     ]
    }
   ],
   "source": [
    "# 转换数据类型\n",
    "# 对整型与浮点型的转换，字符串与整型的转换\n",
    "import numpy as np\n",
    "data = np.array([[1,2,3],[4,5,6]])\n",
    "print(data.dtype)\n",
    "\n",
    "float_data = data.astype(np.float64)  # 数据类型转化为float64\n",
    "print(float_data.dtype) # 输出成功，此时numpy中的数据类型已经重整型转换为浮点型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e1883812-5cc0-4af5-8b78-ac2dd398c5b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.2 2.3 3.5]\n",
      "[1 2 3]\n",
      "int64\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "float_data = np.array([1.2,2.3,3.5])\n",
    "print(float_data)\n",
    "\n",
    "int_data = float_data.astype(np.int64) # 数据类型转换为int64\n",
    "print(int_data)\n",
    "print(int_data.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "21ba348f-1cff-4523-ba25-53cb4092bce7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "int64\n"
     ]
    }
   ],
   "source": [
    "# 如果数组中现在的元素是字符串，且字符串中每个字符都是数字，则也可以使用astype（）方法进行转换\n",
    "import numpy as np\n",
    "str_data = np.array(['1','2','3'])\n",
    "int_data = str_data.astype(np.int64)\n",
    "print(int_data)\n",
    "print(int_data.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60845c47-b990-4400-9eec-17a7c874740b",
   "metadata": {},
   "source": [
    "数组运算\n",
    "矢量化运算\n",
    "在numpy中，大小相等的数组之间的任何算术运算符都会应用到元素级，即只用于位置相同的元素之间，所得结果组成一个新的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f6b1feba-05bd-4c78-ab8e-4941768c096a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  4  6]\n",
      " [ 8 10 12]]\n",
      "[[ 1  4  9]\n",
      " [16 25 36]]\n",
      "[[0 0 0]\n",
      " [0 0 0]]\n",
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "import numpy as np\n",
    "\n",
    "data1 = np.array([[1,2,3],[4,5,6]])\n",
    "data2 = np.array([[1,2,3],[4,5,6]])\n",
    "data_plus = data1 + data2\n",
    "print(data_plus)\n",
    "data_mulity = data1 * data2\n",
    "print(data_mulity)\n",
    "data_dev = data1 - data2\n",
    "print(data_dev)\n",
    "data_c = data1 / data2\n",
    "print(data_c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51545255-18c7-4cda-b21f-1c198bbd98b2",
   "metadata": {},
   "source": [
    "广播机制\n",
    "数组在进行矢量运算时，要求数组的形状是相等的，当形状不相等的数组执行算术运算时，就会出现广播机制\n",
    "广播机制两个或两个以上数组的运算，即使这些数组的shape是不相等的，满足以下一个条件即可\n",
    "1）数组的某一维度等长\n",
    "2）其中一个数组的某一维度为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5563b391-c0cb-4000-835d-93ff1f16f930",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, 1)\n",
      "(3,)\n",
      "[[1 2 3]\n",
      " [2 3 4]\n",
      " [3 4 5]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([[0],[1],[2],[3]])\n",
    "print(arr1.shape)\n",
    "\n",
    "arr2 = np.array([1,2,3])\n",
    "print(arr2.shape)\n",
    "\n",
    "arr_plus = arr1 + arr2\n",
    "print(arr_plus)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb671d3e-0a94-4cdb-8b99-c6e224a72c2d",
   "metadata": {},
   "source": [
    "标量运算\n",
    "大小相等的数组之间的任何算术运算符运用到元素级，同样数组和标量的算术运算符也会将那个标量值传播到各个元素，当数据进行相加，相减、乘或除以一个数字时，这些称为标量运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "daad8a59-6d94-4f32-a25c-0f025700b33c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[11 12 13]\n",
      " [14 15 16]]\n",
      "[[-9 -8 -7]\n",
      " [-6 -5 -4]]\n",
      "[[10 20 30]\n",
      " [40 50 60]]\n",
      "[[0.1 0.2 0.3]\n",
      " [0.4 0.5 0.6]]\n"
     ]
    }
   ],
   "source": [
    "# 数组与标量之间的运算\n",
    "import numpy as np\n",
    "data1 = np.array([[1,2,3],[4,5,6]])\n",
    "data2 = 10\n",
    "data_result1 = data1 + data2\n",
    "print(data_result1)\n",
    "data_result2 = data1 -data2\n",
    "print(data_result2)\n",
    "data_result3 = data1 * data2\n",
    "print(data_result3)\n",
    "data_result4 = data1 / data2\n",
    "print(data_result4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03247744-ef5d-4ecb-a2a1-208122891d07",
   "metadata": {},
   "source": [
    "ndarray的索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0c28ff2b-e603-45a0-bd4a-e7c4b4c435ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7]\n",
      "5\n",
      "[3 4]\n",
      "[1 3 5]\n"
     ]
    }
   ],
   "source": [
    "# 索引和切片\n",
    "# coding:UTF-8\n",
    "import numpy as np\n",
    "arr = np.arange(8)\n",
    "print(arr)\n",
    "print(arr[5])\n",
    "print(arr[3:5])\n",
    "print(arr[1:6:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f75e3280-821e-4af1-a6fe-3eee6c851e8c",
   "metadata": {},
   "source": [
    "对于多维数组来说，索引和切片的使用方法和列表大不一样了。在二维数组中，每个索引位置上的元素不再是一个标量，而是一个一堆一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "25444972-7370-4373-9a15-8220b85ec845",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[4 5 6]\n",
      "2\n",
      "8\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2]\n",
      " [4 5]]\n",
      "[4 5]\n"
     ]
    }
   ],
   "source": [
    "# 多维数组的索引和切片\n",
    "# coding:UTF-8\n",
    "import numpy as np\n",
    "arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]]) \n",
    "print(arr2d)\n",
    "print(arr2d[1]) # 获取索引为1的数组\n",
    "\"\"\"\n",
    "此时如果想通过索引的方式来获取二维数组的单个元素，就需要通过arr[x,y]，以逗号分隔的索引来实现\n",
    "x表示行号，y表示列号\n",
    "\"\"\"\n",
    "print(arr2d[0,1])\n",
    "print(arr2d[2,1])\n",
    "\n",
    "# 通过切片来操作\n",
    "print(arr2d[:2])\n",
    "print(arr2d[0:2,0:2])\n",
    "\n",
    "# 切片和索引混合使用\n",
    "print(arr2d[1, :2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47fa59f2-0451-4bc8-8095-7336e6cf6963",
   "metadata": {},
   "source": [
    "花式（数组)索引的基本使用\n",
    "花式索引是numpy的一个术语，是指将整数数组或列表作为索引，然后根据索引数组或索引列表的每个元素作为目标数组的下标再进行取值。\n",
    "当使用一维数组或列表作为索引时，如果使用索引要操作的目标对象是一维数组，则获取的结果是对应下标的元素；如果要操作的目标对象是一个二维数组，则获取的结果就是对应下标的一行数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "82811db6-cd87-4b6d-b1b4-8b5271d700a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 1. 2. 3.]\n",
      " [1. 2. 3. 4.]\n",
      " [2. 3. 4. 5.]\n",
      " [3. 4. 5. 6.]]\n",
      "[[0. 1. 2. 3.]\n",
      " [1. 2. 3. 4.]\n",
      " [2. 3. 4. 5.]\n",
      " [3. 4. 5. 6.]]\n",
      "[[0. 1. 2. 3.]\n",
      " [1. 2. 3. 4.]\n",
      " [2. 3. 4. 5.]\n",
      " [3. 4. 5. 6.]]\n",
      "[[0. 1. 2. 3.]\n",
      " [1. 2. 3. 4.]\n",
      " [2. 3. 4. 5.]\n",
      " [3. 4. 5. 6.]]\n",
      "[[0. 1. 2. 3.]\n",
      " [2. 3. 4. 5.]]\n",
      "[2. 5.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "demo_arr = np.empty((4,4))\n",
    "for i in range(4):\n",
    "    demo_arr[i] = np.arange(i,i+4)\n",
    "    print(demo_arr)\n",
    "\n",
    "print(demo_arr[[0,2]])\n",
    "\n",
    "demo_arr_result = demo_arr[[1,3],[1,2]]\n",
    "print(demo_arr_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f8a75bc8-a4b7-4a02-8bef-944e8dbd2a46",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张三' '李四' '王五' '赵六']\n",
      "[[79 88 80]\n",
      " [89 90 92]\n",
      " [83 78 80]\n",
      " [78 76 80]]\n",
      "[False False  True False]\n",
      "[[83 78 80]]\n",
      "[[83]]\n"
     ]
    }
   ],
   "source": [
    "# 布尔型索引的基本使用\n",
    "# coding:UTF-8\n",
    "import numpy as np\n",
    "student_name = np.array(['张三','李四','王五','赵六'])\n",
    "print(student_name)\n",
    "student_score = np.array([[79,88,80],[89,90,92],[83,78,80],[78,76,80]])\n",
    "print(student_score)\n",
    "\n",
    "# 对student_name和字符串”王五“通过运算符产生一个布尔型数组\n",
    "print(student_name == '王五')\n",
    "\n",
    "# 将布尔数组作为索引用于存储成绩的数据student_score\n",
    "# 返回的数据是ture值对应的行\n",
    "print(student_score[student_name == '王五'])\n",
    "\n",
    "# 将切片和布尔值混合使用\n",
    "print(student_score[student_name == '王五', :1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "508f7267-a6d9-4e36-9bec-37194b4ac841",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "source": [
    "# 数组的转置与轴对称\n",
    "# coding:UTF-8\n",
    "arr = np.arange(12).reshape(3,4)\n",
    "print(arr)\n",
    "print(arr.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "02c941d9-e8b1-4473-b098-cd446a95d766",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.         1.41421356 1.73205081]\n",
      "[1 2 3]\n",
      "[1 4 9]\n",
      "[23 19 17 23]\n",
      "[132  90  52 120]\n",
      "[12 10 13 15]\n",
      "[ True False  True  True]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.array([1,2,3])\n",
    "# 计算数组的平方根\n",
    "print(np.sqrt(arr))\n",
    "# 计算数组元素的绝对值\n",
    "print(np.abs(arr))\n",
    "# 计算数组元素的平方\n",
    "print(np.square(arr))\n",
    "# 二元通用函数的方法\n",
    "x = np.array([12,9,13,15])\n",
    "y = np.array([11,10,4,8])\n",
    "# 两个数组的和\n",
    "print(np.add(x,y))\n",
    "# 两个数组的积\n",
    "print(np.multiply(x,y))\n",
    "# 两个数组元素级最大值的比较\n",
    "print(np.maximum(x,y))\n",
    "# 执行元素级的比较操作\n",
    "print(np.greater(x,y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "87b228c4-5f6b-4a9c-a0e9-db9339d38eea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[22 28]\n",
      " [49 64]]\n",
      "[[7 0 0]\n",
      " [0 8 0]\n",
      " [0 0 9]]\n",
      "24\n"
     ]
    }
   ],
   "source": [
    "# 线性运算\n",
    "# coding:UTF-8\n",
    "import numpy as np\n",
    "arr1 = np.array([[1,2,3],[4,5,6]])\n",
    "arr2 = np.array([[1,2],[3,4],[5,6]])\n",
    "# 使用numpy.dot进行矩阵的乘法\n",
    "result = np.dot(arr1,arr2)\n",
    "print(result)\n",
    "\n",
    "# 创建对角矩阵\n",
    "D = np.diag([7,8,9])\n",
    "print(D)\n",
    "# 矩阵的迹\n",
    "trace1 = np.trace(D)\n",
    "print(np.trace(D))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "50968761-fdac-4417-8340-53a03715300b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17.999999999999996\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "matric = np.array([[4,7],[2,8]])\n",
    "# 计算行列式\n",
    "determinant = np.linalg.det(matric)\n",
    "print(determinant)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "246b0e7a-3b4b-4ae2-82a7-55d8351d1264",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "[[4 5 6]\n",
      " [7 8 9]]\n",
      "数组b的形状： (2, 3)\n",
      "数组a的数据类型： int32\n",
      "[3 4 5]\n",
      "[2 4 6]\n",
      "[0.84147098 0.90929743 0.14112001]\n",
      "[ 2.71828183  7.3890561  20.08553692]\n",
      "[[1.38629436 1.60943791 1.79175947]\n",
      " [1.94591015 2.07944154 2.19722458]]\n",
      "[[-2.   1. ]\n",
      " [ 1.5 -0.5]]\n",
      "-2.0000000000000004\n",
      "[-0.37228132  5.37228132]\n",
      "[[-0.82456484 -0.41597356]\n",
      " [ 0.56576746 -0.90937671]]\n",
      "[ 0.59289803 -0.20072958  0.93504614  1.20595076 -0.227846   -1.65118423\n",
      "  0.90230394  1.48465672  0.61696973 -0.72157567]\n",
      "-0.09528548644472855\n",
      "0.9840124024256672\n",
      "-0.08968272747555843\n",
      "1.7872241933643755\n",
      "-2.2145979284260076\n",
      "[1.         1.41421356 1.73205081]\n",
      "[1 4 9]\n",
      "沿b轴0的总和： [11 13 15]\n",
      "沿b轴1的乘积： [120 504]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([[4,5,6],[7,8,9]])\n",
    "print(a)\n",
    "print(b)\n",
    "print(\"数组b的形状：\",b.shape)\n",
    "print(\"数组a的数据类型：\",b.dtype)\n",
    "# 数组操作\n",
    "c = a+2\n",
    "d = a*2\n",
    "e = np.sin(a)\n",
    "f = np.exp(a)  # 指数函数\n",
    "g = np.log(b)  \n",
    "\n",
    "print(c)\n",
    "print(d)\n",
    "print(e)\n",
    "print(f)\n",
    "print(g)\n",
    "\n",
    "# 线性代数\n",
    "matrix = np.array([[1,2],[3,4]])\n",
    "inv_matrix = np.linalg.inv(matrix) # 逆矩阵\n",
    "det_matrix = np.linalg.det(matrix) # 行列式\n",
    "eigenvalues,eigenvectors = np.linalg.eig(matrix) # 特征值和特征向量\n",
    "print(inv_matrix)\n",
    "print(det_matrix)\n",
    "print(eigenvalues)\n",
    "print(eigenvectors)\n",
    "\n",
    "# 统计\n",
    "h = np.random.randn(100) # 生成标准正态分布的随机样本\n",
    "mean_h = np.mean(h) # 平均值\n",
    "sid_h = np.std(h)  # 标准差\n",
    "median_h = np.median(h) # 中位数\n",
    "max_h = np.max(h) # 最大值\n",
    "min_h = np.min(h) # 最小值\n",
    "print(h[:10])\n",
    "print(mean_h)\n",
    "print(sid_h)\n",
    "print(median_h)\n",
    "print(max_h)\n",
    "print(min_h)\n",
    "\n",
    "# 数学函数\n",
    "i = np.sqrt(a)\n",
    "j = np.power(a,2)\n",
    "k = np.sum(b,axis = 0)\n",
    "l = np.prod(b,axis = 1)\n",
    "print(i)\n",
    "print(j)\n",
    "print(\"沿b轴0的总和：\",k)\n",
    "print(\"沿b轴1的乘积：\",l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "51472b4d-e7f7-471f-b3ca-ad8aca4c0b09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "[0.5488135  0.71518937 0.60276338 0.54488318 0.4236548 ]\n",
      "None\n",
      "[0.5488135  0.71518937 0.60276338 0.54488318 0.4236548 ]\n",
      "[0.64589411 0.43758721 0.891773   0.96366276 0.38344152]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "import numpy as np\n",
    "np_seed = np.random.seed(0)\n",
    "np_rand = np.random.rand(5)\n",
    "print(np_seed)\n",
    "print(np_rand)\n",
    "\n",
    "np_seed1 = np.random.seed(0)\n",
    "np_rand1 = np.random.rand(5)\n",
    "print(np_seed1)\n",
    "print(np_rand1)\n",
    "\n",
    "\n",
    "np_rand2 = np.random.rand(5)\n",
    "\n",
    "print(np_rand2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d59fdd5a-612d-42fd-abd5-ce8fd1125623",
   "metadata": {},
   "source": [
    "由此可见，seed（）函数使得随机值具有预见性，当传递的是参数不同或者不传递参数时，则该函数的作用跟rand（）相同，即多次生成随机数且每次生成的随机数都不同。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "1187e3ed-4282-4a97-bada-1d37d464c178",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5488135039273248\n",
      "5\n",
      "[[0.84426575 0.85794562 0.84725174]\n",
      " [0.6235637  0.38438171 0.29753461]\n",
      " [0.05671298 0.27265629 0.47766512]]\n",
      "[[-0.08767307  1.36687937]\n",
      " [ 1.1253141  -0.35899555]]\n",
      "[[0.61936959 0.49056207]\n",
      " [0.21989816 0.35560928]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 设置随机种子以保证结果可复现\n",
    "np.random.seed(0)\n",
    "# 生成一个【0，1】之间的均匀分布随机数\n",
    "random_float = np.random.rand()\n",
    "print(random_float)\n",
    "# 生成一个【0，10）之间的整数\n",
    "random_integer = np.random.randint(0,10)\n",
    "print(random_integer)\n",
    "# 生成一个形状为（3，3）的数组，其中元素时【0，1）之间的均匀分布随机数\n",
    "random_array = np.random.rand(3,3)\n",
    "print(random_array)\n",
    "# 生成一个形状为（2，2）的数组，其中元素时从正态分布N（0，1）中抽取的随机数\n",
    "normal_array = np.random.normal(loc=0.0,scale=1.0,size=(2,2))\n",
    "print(normal_array)\n",
    "# 生成一个形状为（2，2）的数组，其中元素时从beta分布beta（2，5）中抽取的随机数\n",
    "beta_array = np.random.beta(a=2,b=5,size=(2,2))\n",
    "print(beta_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8eec9fa6-38b0-4991-81f8-656f7c399bee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
